/* 
 * Created on 2007-2-20
 * Last modified on 2007-2-20
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.log4j.Logger;

public class BeanUtils {

	private static final Logger logger = Logger.getLogger(BeanUtils.class);
	
	static {
		// 注册sql.date的转换器，即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
		ConvertUtils.register(
				new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class);
		
		ConvertUtils.register(
						new org.apache.commons.beanutils.converters.SqlTimestampConverter(
								null), java.sql.Timestamp.class);
		// 注册util.date的转换器，即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
		
		ConvertUtils.register(
				new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.util.Date.class);
	}
	public static class A {
		private String s;
		private Date d;
		public String getS() {
			return s;
		}
		public void setS(String s) {
			this.s = s;
		}
		public Date getD() {
			return d;
		}
		public void setD(Date d) {
			this.d = d;
		}
	}
	public static void main(String[] args) {
		A a = new A();
		a.setD(new Date());
		a.setS("sss");
		//
		A a1 = new A();
		BeanUtils.copyProperties(a1, a);
		System.out.println(a1.getS());
		System.out.println(a1.getD());
	}

	public static void copyProperties(Object target, Object original) {
		if (ValidatorHelper.isEmpty(target)
				|| ValidatorHelper.isEmpty(original)) {
			target = null;
			original = null;
			return;
		}
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperties(target,
					original);
		} catch (IllegalAccessException e) {
			logger.error(target.getClass().getName());
			logger.error(original.getClass().getName());
			logger.error(e.toString());
		} catch (InvocationTargetException e) {
			logger.error(target.getClass().getName());
			logger.error(original.getClass().getName());
			logger.error(e.toString());
		} catch (Exception e) {
			logger.error(target.getClass().getName());
			logger.error(original.getClass().getName());
			logger.error(e.toString());
		}
	}

}
